package com.lee.chapter01;

public class UnsafeValueHolder {
    public Holder holder;

    public void initialize() {
        holder = new Holder(42);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            UnsafeValueHolder valueHolder = new UnsafeValueHolder();
            Thread thread1 = new Thread(valueHolder::initialize);
            Thread thread2 = new Thread(() -> {
            if (valueHolder.holder != null) {
                valueHolder.holder.assertSanity();
            }
            });
            thread2.start();
            thread1.start();
        }

    }
}

class Holder {
    private int n;

    public Holder(int n) {
        this.n = n;
    }

    public void assertSanity() {
        if (n != n) {
            throw new AssertionError("This statement is false.");
        } else {
            System.out.println("This statement is true.");
        }
    }
}
